package cn.itrip.search.service;

import cn.itrip.beans.vo.hotel.SearchHotelVO;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.Page;
import cn.itrip.common.PropertiesUtils;
import cn.itrip.search.beans.vo.hotel.ItripHotelVO;
import cn.itrip.search.dao.BaseQuery;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SearchHotelServiceImpl implements SearchHotelService {
    public static String URL = PropertiesUtils.get("database.properties","baseUrl");

    private BaseQuery<ItripHotelVO> itripHotelVOBaseQuery = new BaseQuery(URL);


    @Override
    public Page<ItripHotelVO> searchItripHotelPage(SearchHotelVO vo, Integer pageNo, Integer pageSize) throws Exception {
        SolrQuery query = new SolrQuery("*:*");
        StringBuilder qQuery = new StringBuilder();
        if(EmptyUtils.isNotEmpty(vo)){
            //text 文本框模糊查询
            qQuery.append(" destination:"+vo.getDestination());
                if(EmptyUtils.isNotEmpty(vo.getKeywords())){
                    qQuery.append(" AND keyword:"+vo.getKeywords());
                }
                if(EmptyUtils.isNotEmpty(qQuery)){ //如果前面条件不为空就set进去
                    query.setQuery(qQuery.toString());
                }

                //酒店等级
                if(EmptyUtils.isNotEmpty(vo.getHotelLevel())){
                    query.addFilterQuery(" hotelLevel:"+vo.getHotelLevel()+" ");
                }
                //酒店特色id
                if(EmptyUtils.isNotEmpty(vo.getFeatureIds())){
                    StringBuilder buffer = new StringBuilder("(");
                    int flag =0;
                    String featureIdArray[] = vo.getFeatureIds().split(",");
                    for (String featureId:featureIdArray) {
                        if(flag==0){
                            buffer.append(" featureIds:"+"*,"+featureId+",*");
                        }else{
                            buffer.append(" OR featureIds:"+"*,"+featureId+",*");
                        }
                        flag++;
                    }
                    buffer.append(")");
                    query.addFilterQuery(buffer.toString());
                }
                //商圈ID
                if(EmptyUtils.isNotEmpty(vo.getTradeAreaIds())){
                    StringBuilder buffer = new StringBuilder("(");
                    int flag = 0;
                    String tradeAreaIdArray[] = vo.getTradeAreaIds().split(",");
                    for (String tradeAreaId:tradeAreaIdArray) {
                        if(flag==0){
                            buffer.append(" tradingAreaIds:"+"*,"+tradeAreaId+",*");
                        }else {
                            buffer.append(" OR tradingAreaIds:"+"*,"+tradeAreaId+",*");
                        }
                        flag++;
                    }
                    buffer.append(")");
                    query.addFilterQuery(buffer.toString());
                }
                //价格
                if(EmptyUtils.isNotEmpty(vo.getMaxPrice())){
                    query.addFilterQuery("minPrice:" + "[* TO "+vo.getMaxPrice() + "]");
                }
                if(EmptyUtils.isNotEmpty(vo.getMinPrice())){
                    query.addFilterQuery("minPrice:" + "["+vo.getMinPrice() + " TO *]");
                }
                //按某字段排序
                if(EmptyUtils.isNotEmpty(vo.getAscSort())){
                    query.addSort(vo.getAscSort(),SolrQuery.ORDER.asc);
                }
                if(EmptyUtils.isNotEmpty(vo.getDescSort())){
                    query.addSort(vo.getDescSort(),SolrQuery.ORDER.desc);
                }
        }
        Page<ItripHotelVO> page = itripHotelVOBaseQuery.queryPage(query,pageNo,pageSize,ItripHotelVO.class);
        return page;
    }

    @Override
    public List<ItripHotelVO> searchItripHotelListByHotCity(Integer cityId, Integer pageSize) throws Exception {
        SolrQuery query = new SolrQuery("*:*");
        if(EmptyUtils.isNotEmpty(cityId)){
            query.addFilterQuery(" cityId: "+cityId);
        }else {
            return null;
        }
        List<ItripHotelVO> hotelVOList = itripHotelVOBaseQuery.queryList(query,pageSize,ItripHotelVO.class);
        return hotelVOList;
    }
}
